define(['./FloatRect'], function(FloatRect) {

	var FormPage = function() {
		this.formWidgets = [];		
		this.rotate = 0;
		this.pageWidth = 0;
        this.pageHeight = 0;
        this.pdfRect = new FloatRect();
	};

	var fn = FormPage.prototype;

   
	fn.insertFormWidget = function(formWidget){
        if (!formWidget){
            throw 'formWidget should not be invalid';
        }
		this.formWidgets.push(formWidget);
	};

     //根据widget的坐标进行插入，并设置tabNum
     //5为同一行纠正值，如果两个元素的top之差的绝对值小等于5,认为是同一行
    fn.sortFormWidget = function() {
        var formWidgets = this.formWidgets;
        this.formWidgets.sort(function(v1, v2){
            if (v1 === null || v2 === null){
                console.log(v1, v2, formWidgets);
            }
           if (v1.rect.top < (v2.rect.top - 5)) return -1;   //上一行
           if ((Math.abs(v1.rect.top - v2.rect.top)) <= 5){  //同一行,比较left值               
                return (v1.rect.left - v2.rect.left);
           }
           return 1;
        });

        var _tabNum = 0;
        angular.forEach(this.formWidgets, function(value){
            if (!value.tabNum){
                value.tabNum = _tabNum + 1;                
            }
            _tabNum = value.tabNum;
        });
    };

	fn.transform = function (pdf_l, pdf_b, pdf_w, pdf_h) {
        var retFloatRect = new FloatRect();
        switch (this.rotate) {
            //90
            case 1:
            {
                retFloatRect.top = pdf_l - this.pdfRect.left;
                retFloatRect.left = pdf_b - this.pdfRect.top;
                retFloatRect.width = pdf_h;
                retFloatRect.height = pdf_w;
            }
                break;
            //180
            case 2:
            {
                retFloatRect.top = pdf_b - this.pdfRect.top;
                retFloatRect.left = this.pdfRect.width - pdf_l - this.pdfRect.left - pdf_w;
                retFloatRect.width = pdf_w;
                retFloatRect.height = pdf_h;
            }
                break;
            case 3:
                //270
            {
                retFloatRect.top = this.pdfRect.width - (pdf_l - this.pdfRect.left) - pdf_w;
                retFloatRect.left = this.pdfRect.height - (pdf_b - this.pdfRect.top) - pdf_h;
                retFloatRect.width = pdf_h;
                retFloatRect.height = pdf_w;
            }
                break;
            default:
                //0
            {
                retFloatRect.top = this.pdfRect.height - (pdf_b - this.pdfRect.top) - pdf_h;
                retFloatRect.left = pdf_l - this.pdfRect.left;
                retFloatRect.width = pdf_w;
                retFloatRect.height = pdf_h;
            }
        }

        return retFloatRect;
    };

    fn.getFormWidgetByName = function(widgetName){
        for (var i = 0; i < this.formWidgets.length; i++){
            if (this.formWidgets[i].name === widgetName){
                return this.formWidgets[i];
            }
        }
        return null;
    };

	return FormPage;
});